package com.dog.auth.service.impl;

import com.dog.auth.config.captcha.CaptchaProperties;
import com.dog.auth.domain.dto.request.LoginReqDTO;
import com.dog.auth.domain.dto.request.RegisterReqDTO;
import com.dog.auth.domain.vo.AuthUserVO;
import com.dog.auth.service.AuthService;
import com.dog.auth.service.ImageCodeService;
import com.dog.framework.core.constant.SecurityConstants;
import com.dog.framework.core.model.JwtUser;
import com.dog.framework.core.model.ResponseResult;
import com.dog.framework.core.utils.BeanCopyUtils;
import com.dog.framework.core.utils.JwtUtils;
import com.dog.framework.redis.service.RedisService;
import com.dog.framework.web.util.SecurityUserUtils;
import com.dog.modules.system.api.feign.SysUserFeignClient;
import com.dog.modules.system.api.vo.SysUserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author liuxk
 * @date 2024/1/5 17:39
 *
 */
@Service
public class AuthServiceImpl  implements AuthService {

    @Autowired
    private CaptchaProperties captchaProperties;

    @Autowired
    private ImageCodeService imageCodeService;

    @Autowired
    private SysUserFeignClient sysUserFeignClient;

    @Autowired
    private RedisService redisService;


    @Override
    public Map<String, Object> createCaptcha() throws IOException {
        return imageCodeService.createCaptcha();
    }

    @Override
    public AuthUserVO login(LoginReqDTO loginReqDTO) {

        //  验证码关闭，不处理，验证码打开，判断是否正确。
        if (captchaProperties.getEnabled()) {
            imageCodeService.checkCaptcha(loginReqDTO.getImageCode(), loginReqDTO.getUuid());
        }

        //验证账号密码是否正常


        //验证登录失败次数是否超过

        SysUserVO sysUserVO =  sysUserFeignClient.findByUsername(loginReqDTO.getUsername());

        //模拟数据库查询的用户
        JwtUser tagetUser = new JwtUser();
        tagetUser.setUserId(sysUserVO.getId());
        tagetUser.setUsername(sysUserVO.getUsername());
        tagetUser.setOrgId(sysUserVO.getOrgId());
        tagetUser.setTenantId(1001L);
        List<String> roles = Stream.of("common","admin").collect(Collectors.toList());
        tagetUser.setRoleCodeList(roles);
        tagetUser.setPassword(sysUserVO.getPassword());

        // 生成token
        String token = JwtUtils.generateToken(tagetUser);
        AuthUserVO authUserVO = BeanCopyUtils.copy(sysUserVO,AuthUserVO.class);
        authUserVO.setToken(token);

        return authUserVO;
    }

    @Override
    public boolean register(RegisterReqDTO dto) {
        SysUserVO sysUserVO = BeanCopyUtils.copy(dto, SysUserVO.class);
        return sysUserFeignClient.save(sysUserVO);
    }

    @Override
    public boolean logout() {
        JwtUser loginVal = SecurityUserUtils.getCurrentUser();
        //这个jti放入redis中，并且过期时间设置为token的过期时间
        redisService.setCacheObject(SecurityConstants.JWT_JTI_KEY_PREFIX + loginVal.getJti(),"",loginVal.getExpireIn(), TimeUnit.SECONDS);
        return Boolean.TRUE;
    }

    @Override
    public ResponseResult refresh(HttpServletRequest request) {
        return null;
    }
}
